package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    //todo 显示购物车数据
    @Override
    public R findByUserId() {
        Long userId = BaseContext.threadLocal.get();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
        return R.success(shoppingCarts);
    }
    //todo 清空购物车
    @Override
    public void clean() {
        //获得登录用户的id
        Long aLong = BaseContext.threadLocal.get();
        //找到对应用户Id加入购物车的菜品,并删除
        LambdaUpdateWrapper<ShoppingCart> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,aLong);
        shoppingCartMapper.delete(wrapper);
    }
    //todo 删除购物车单个商品
    @Override
    public R sub(ShoppingCart shoppingCart) {
        Long userId = BaseContext.threadLocal.get();
        if (shoppingCart.getDishId() != null){
            LambdaQueryWrapper<ShoppingCart> dishWrapper = new LambdaQueryWrapper<>();
            dishWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            dishWrapper.eq(ShoppingCart::getUserId,userId);
            ShoppingCart shoppingCartl = shoppingCartMapper.selectOne(dishWrapper);
            if (shoppingCartl.getNumber()==1){
                shoppingCartMapper.delete(dishWrapper);
            }else {
                shoppingCartl.setNumber(shoppingCartl.getNumber()-1);
                shoppingCartMapper.updateById(shoppingCartl);
            }
        }
        if (shoppingCart.getSetmealId() != null){
            LambdaQueryWrapper<ShoppingCart> setmealWrapper = new LambdaQueryWrapper<>();
            setmealWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            setmealWrapper.eq(ShoppingCart::getUserId,userId);
            ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(setmealWrapper);
            if (shoppingCart1.getNumber()==1){
                shoppingCartMapper.delete(setmealWrapper);
            }else {
                shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
                shoppingCartMapper.updateById(shoppingCart1);
            }
        }
        return R.success("删除成功");
    }

    //todo 添加菜品购物车
    @Override
    public R add(ShoppingCart shoppingCart) {
        //补全参数
        shoppingCart.setUserId(BaseContext.threadLocal.get());
        //根据当前用户登录id及本次添加的菜品id/套餐id ,查询购物车数据是否存在
        if (shoppingCart.getDishId() != null){
            //查询此菜品是否存在购物车中,如果存在执行修改数量操作
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            //添加条件,菜品id相等
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            //添加条件,当前用户登录id相等
            wrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
            //添加口味条件
            if (shoppingCart.getDishFlavor() != null) {
                wrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
            }
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
            insertOrUpdate(shoppingCarts,shoppingCart);
        }else {//代表套餐
            //查询此菜品是否存在购物车中,如果存在执行修改数量操作
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            //添加条件,菜品id相等
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            //添加条件,当前用户登录id相等
            wrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
            insertOrUpdate(shoppingCarts,shoppingCart);
        }
        //todo 添加或者修改成功后,记得把修改或者添加的对象返回到前端
        if (shoppingCart.getDishId() != null && shoppingCart.getDishFlavor() != null){
            shoppingCart.setNumber(null);
            return R.success(shoppingCart);

        }else {
            return R.success(shoppingCart);
        }
    }

    private void insertOrUpdate(List<ShoppingCart> shoppingCarts,ShoppingCart shoppingCart){
        if (shoppingCarts.size() == 0){//代表数据库没有
            shoppingCart.setNumber(1);
            //添加功能即可
            shoppingCartMapper.insert(shoppingCart);
        }else {
            //数量+1
            ShoppingCart shoppingCartDB = shoppingCarts.get(0);
            shoppingCartDB.setNumber(shoppingCartDB.getNumber()+1);
            //存储到数据库中
            shoppingCartMapper.updateById(shoppingCartDB);
            shoppingCart.setNumber(shoppingCartDB.getNumber());
            shoppingCart.setAmount(shoppingCartDB.getAmount());
            shoppingCart.setDishFlavor(shoppingCartDB.getDishFlavor());
            shoppingCart.setDishId(shoppingCartDB.getDishId());
            shoppingCart.setSetmealId(shoppingCartDB.getSetmealId());
            shoppingCart.setImage(shoppingCartDB.getImage());
        }
    }
}
